package com.zzg.service.impl;

import java.util.List;

import com.zzg.bean.Nanny;
import com.zzg.bean.Page;
import com.zzg.dao.NannyDAO;
import com.zzg.dao.NannyTypeDAO;
import com.zzg.dao.impl.NannyDAOImpl;
import com.zzg.dao.impl.NannyTypeDAOImpl;
import com.zzg.service.NannyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NannyServiceImpl implements NannyService{
	@Autowired
	private NannyDAO nannydao;
	
	@Override
	public void addNanny(Nanny nanny) {
		nannydao.addNanny(nanny);
	}

	@Override
	public void delNanny(int id) {
		nannydao.delNanny(id);
	}

	@Override
	public void updNanny(Nanny nanny) {
		nannydao.updNanny(nanny);
	}

	@Override
	public Nanny findNannyById(int id) {
		return nannydao.findNannyById(id);
	}

	@Override
	public List<Nanny> findAllNanny() {
		return nannydao.findAllNanny();
	}

	@Override
	public List<Nanny> findNannyLikeName(String name) {
		return nannydao.findNannyLikeName(name);
	}

	@Override
	public Page<Nanny> selectNannyPage(int currentPage) {
		int count = nannydao.selectCount();
		int pageCount = count % Page.PAGE_SIZE == 0?count/Page.PAGE_SIZE:count/Page.PAGE_SIZE+1;
		List<Nanny> data = nannydao.selectNannysLimit((currentPage-1)*Page.PAGE_SIZE, Page.PAGE_SIZE);
		NannyTypeDAO typeDAO = new NannyTypeDAOImpl();
		for(Nanny nanny :data){
			nanny.setNanny_type(typeDAO.findNannyTypeById(nanny.getNanny_type_id()));
		}
		Page<Nanny> page = new Page<>();
		page.setCurrentPage(currentPage);
		page.setPageCount(pageCount);
		page.setData(data);
		return page;
	}
	
}
